//
//  LZYProgressHUD.m
//  DigitalCampus
//
//  Created by liuzhanyu on 16/6/19.
//  Copyright © 2016年 刘占宇. All rights reserved.
//

#import "LZYProgressHUD.h"
@interface LZYProgressHUD ()
{
    UIImageView *imageView;
}
@end

@implementation LZYProgressHUD


+ (LZYProgressHUD*)sharedView {
    static dispatch_once_t once;
    static LZYProgressHUD *sharedView;
    dispatch_once(&once, ^ { sharedView = [[LZYProgressHUD alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; });
    return sharedView;
}

+ (void)showProgressAnimation{
    [[[self sharedView] imageView] startAnimating];
}


- (UIImageView *)imageView {
    if (imageView == nil)
        imageView = [[UIImageView alloc] initWithFrame:CGRectMake(DISPATCH_MEMORYPRESSURE_CRITICAL *0.5 - 50, SCREEN_H *0.5 - 50, 100, 100)];
    NSMutableArray *gifarr = [[NSMutableArray alloc]init];
    if(!imageView.superview)
        
        //UIImageView *gifImageView = [[UIImageView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        for (int i = 1; i < 5; i++) {
            NSString *IMGname = [NSString stringWithFormat:@"%d.png",i];
            NSLog(@"----========%@",IMGname);
            UIImage *img = [UIImage imageNamed:IMGname];
            [gifarr addObject:img];
        }
    imageView.animationImages = gifarr; //动画图片数组
    imageView.animationDuration = 0.5; //执行一次完整动画所需的时长
    imageView.animationRepeatCount = 10;  //动画重复次数
    //[imageView startAnimating];
    [self addSubview:imageView];
    [[[UIApplication sharedApplication] keyWindow] addSubview:self];
    return imageView;
}


- (void)dismiss {
    [UIView animateWithDuration:0.15
                          delay:0
                        options:UIViewAnimationCurveEaseIn | UIViewAnimationOptionAllowUserInteraction
                     animations:^{
                         self.transform = CGAffineTransformScale(self.transform, 0.8, 0.8);
                         self.alpha = 0;
                     }
                     completion:^(BOOL finished){
                         if(self.alpha == 0) {
                             [[NSNotificationCenter defaultCenter] removeObserver:self];
                             [imageView removeFromSuperview];
                             
                             [self removeFromSuperview];
                             //[[UIApplication sharedApplication] keyWindow] = nil;
                             
                             // uncomment to make sure UIWindow is gone from app.windows
                             //NSLog(@"%@", [UIApplication sharedApplication].windows);
                             //NSLog(@"keyWindow = %@", [UIApplication sharedApplication].keyWindow);
                         }
                     }];
}



@end
